package com.example.baselibs.public_fun;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

import com.blankj.utilcode.util.NetworkUtils;
import com.example.baselibs.public_fun.interfaces.VersionUpdateImpl;
import com.example.baselibs.utils.ActivityUtils;

/**
 * 下载apk
 */
public class DownLoadApk{
    private static Context mContext;
    /**
     * 检查网络是否链接，链接类型
     */
    public static void checkNetworkState(Context  context, String url, int i, String apkPath){
        mContext=context;
        if(NetworkUtils.isConnected()){
            if (NetworkUtils.isMobileData()){
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("下载提示");
                builder.setMessage("您正在使用流量下载应用，是否继续？");
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("继续", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //继续下载
                        VersionUpdate.checkVersion((VersionUpdateImpl) context,url,i,apkPath);
                    }
                });
                builder.create().show();
            }
            if (NetworkUtils.isWifiConnected()){
                //下载
                VersionUpdate.checkVersion((VersionUpdateImpl) context,url,i,apkPath);
                Log.i("bbq","进入WiFi下载");
            }

        }else {
            ActivityUtils.showToast(context,"网络不可用，请设置网络！");
            NetworkUtils.openWirelessSettings();
        }
    }

    /*Intent intent;
    @Override
    public void bindService(String apkUrl, int i, String apkPath) {
        Intent intent = new Intent(mContext, DownLoadService.class);
        intent.putExtra(DownLoadService.BUNDLE_KEY_DOWNLOAD_URL, apkUrl);
        intent.putExtra(DownLoadService.BUNDLE_KEY_APK_PATH, apkPath);
        intent.putExtra("index", i);
        mContext.startService(intent);
    }

    public void stopService(){
        if (intent!=null){
            mContext.stopService(intent);
        }

    }*/
}
